package com.runshu.newsmartark.util.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.runshu.newsmartark.R;
import com.runshu.newsmartark.adapter.TestArrayAdapter;
import com.runshu.newsmartark.entity.UserInfoEntity;
import com.runshu.newsmartark.mvp.contract.CallBackObJValue;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/7/24
 * @decs：
 */

public class ComChoseNameDia extends Dialog {
    @BindView(R.id.tv_dialog_show)
    TextView mTvDialogShow;
    @BindView(R.id.spinner)
    Spinner mSpinner;
    @BindView(R.id.img_cancle)
    TextView mImgCancle;
    @BindView(R.id.img_sure)
    TextView mImgSure;

    private Context mContext;
    private View.OnClickListener onClickListenerCancel;
    private View.OnClickListener onClickListenerEnsure;
    private CallBackObJValue callBackObJValue;
    private List<String> list = new ArrayList<String>();
    private ArrayAdapter<String> adapter;

    private List<UserInfoEntity> mlist;

    public ComChoseNameDia(@NonNull Context context, CallBackObJValue listener, List<UserInfoEntity> mlist) {
        super(context, R.style.dialog_untran);
        mContext = context;
        callBackObJValue = listener;
        this.mlist = mlist;
        setContentView(R.layout.dialog_chose_name);
        ButterKnife.bind(this);

    }


    @Override
    public void show() {

        setButtonOnClickListener(mImgCancle, onClickListenerCancel);
        setButtonOnClickListener(mImgSure, onClickListenerEnsure);

        for (int i = 0; i < mlist.size(); i++) {
            list.add(mlist.get(i).getUserName());
        }
        adapter = new TestArrayAdapter(mContext, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter);

        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                callBackObJValue.SendMessageValue(mlist.get(i).getUserCode());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        super.show();
    }

    private void setButtonOnClickListener(TextView textView, View.OnClickListener onClickListener) {
        if (textView == null) {
            return;
        }
        if (onClickListener == null) {
            onClickListener = onClickListenerDismiss;
        }
        textView.setOnClickListener(onClickListener);

    }

    private View.OnClickListener onClickListenerDismiss = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    };


    /**
     * 确定按钮事件监听 默认是dismiss对话框
     *
     * @param onClickListenerEnsure
     */
    public void setOnClickListenerEnsure(View.OnClickListener onClickListenerEnsure) {
        this.onClickListenerEnsure = onClickListenerEnsure;
    }

    /**
     * 取消按钮事件监听 默认是dismiss对话框
     *
     * @param onClickListenerCancel
     */
    public void setOnClickListenerCancel(View.OnClickListener onClickListenerCancel) {
        this.onClickListenerCancel = onClickListenerCancel;
    }


}